<?php

namespace app\app\controller;

use app\app\model\ProjectCheckModel;
use app\app\model\ProjectGGKTypeModel;
use app\app\model\ProjectLZModel;
use app\BaseController;
use app\app\model\ProjectGGKModel;
use app\Request;

class ProjectProblem extends BaseController
{

    public function index(Request $request)
    {
        $data = $request->param();
        $start_time = "2021-12-07 00:00:00";
        $end_time = "2021-12-15 23:59:59";
        $where = "lz_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        $where .= " AND check_count >0";

        if (isset($data['user_name'])) $where .= " AND user_name  LIKE '%" . $data['user_name'] . "%'";
        $list = ProjectLZModel::field('user_name,SUM(check_count) as total')->where($where)->group('user_name')->select()->toarray();

        $total = array_sum(array_map(function ($val) {
            return $val['total'];
        }, $list));
        $res = array(
            'total' => $total,
            'list' => $list
        );
        $this->success('success', $res);
    }


    public function list(Request $request)
    {
        $data = $request->param();
        $page_size = input('pageSize', '20');

        $where = "1=1";
        if (isset($data['start_time'])) $where .= " AND b.lz_time BETWEEN  '" . $data['start_time'] . "'  AND '" . $data['end_time'] . "'";
        if (isset($data['user_name'])) $where .= " AND b.user_name  = '" . $data['user_name'] . "'";
        if (isset($data['project_id'])) $where .= " AND a.project_id  = '" . $data['project_id'] . "'";
        $list = ProjectCheckModel::alias('a')->field('a.check_type,a.zrr_name,a.fk_money,c.project_name,d.file_path,e.peccancy_type')
            ->leftjoin('t_project_lz b', 'b.id = a.project_lz_id')
            ->leftjoin('t_project c', 'c.id = a.project_id')
            ->leftjoin('t_project_image d', 'd.id = a.check_photo_id')
            ->leftjoin('t_project_peccancy e', 'e.id = a.peccancy_id')
            ->where($where)->paginate($page_size);

        foreach ($list as $key => $value) {
            if ($value['check_type'] == 1) {
                $value['check_type'] = "质量问题";
            } else {
                $value['check_type'] = "安全问题";
            }
        }
        $this->success('success', $list);

    }


}
